import common.CommonDemo;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.print.PrintOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;

public class WindowManagement {

    private CommonDemo commonDemo;
    private WebDriver driver;

    @BeforeTest
    public void setUp() throws Exception {
        commonDemo = new CommonDemo();
        driver = commonDemo.getChromeDriver();
        String url = commonDemo.getUserDir("/src/main/resources/windowHandle.html");
        driver.get(url);
    }
    /**
     * 获取浏览器的宽和高
     */
    @Test
    public void getWindowSize(){

        int width = driver.manage().window().getSize().getWidth();
        int height = driver.manage().window().getSize().getHeight();
        System.out.println(width+":::"+height);
    }

    /**
     * 获取浏览器的宽和高
     */
    @Test
    public void getWindowSizeWithDimension(){
        Dimension dimension = driver.manage().window().getSize();
        int width = dimension.getWidth();
        int height = dimension.getHeight();
        System.out.println(width+":::"+height);
    }

    /**
     * 设置浏览器的宽和高
     */
    @Test
    public void setWindowSize(){
        Dimension dimension = new Dimension(1500,2000);
        driver.manage().window().setSize(dimension);

    }

    @Test
    public void getWindowPosition(){
        int x = driver.manage().window().getPosition().getX();
        int y = driver.manage().window().getPosition().getY();
        System.out.println(x+":::"+y);
    }

    @Test
    public void getWindowPositionWithPosition(){
        Point position = driver.manage().window().getPosition();
        int x = position.getX();
        int y = position.getY();
        System.out.println(x+"::"+y+"::"+position.x+ position.y);
    }

    /**
     * 移动窗口到指定位置
     */
    @Test
    public void setWindowPosition(){
        Point point = new Point(0, 0);
        driver.manage().window().setPosition(point);
    }

    /**
     * 浏览器最大化
     */
    @Test
    public void maximizeWindow(){
        driver.manage().window().maximize();
    }

    /**
     * 浏览器最小化
     */
    @Test
    public void minimizeWindow(){
        driver.manage().window().minimize();
    }

    /**
     * 浏览器全屏
     */
    @Test
    public void fullscreenWindow(){
        driver.manage().window().fullscreen();
    }

    /**
     * 截屏
     */
    @Test
    public void takeScreenshot() throws IOException {
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcFile, new File("./image.png"));
    }

    /**
     *
     * 元素截图
     * @throws IOException
     */
    @Test
    public void takeElementScreenshot() throws IOException{
        WebElement element = driver.findElement(By.id("baidu"));
        File srcFile = element.getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcFile, new File("./image2.png"));
    }

    /**
     * 执行脚本操作点击
     */
    @Test
    public void executeScript(){
        JavascriptExecutor js = (JavascriptExecutor)driver;
        WebElement element = driver.findElement(By.id("image"));
        js.executeScript("arguments[0].click();", element);
    }

    /**
     * 执行脚本获取文本内容
     */
    @Test
    public void executeScript2(){
        JavascriptExecutor js = (JavascriptExecutor)driver;
        WebElement element = driver.findElement(By.id("btn"));
        String text = (String) js.executeScript("return arguments[0].innerText", element);
        System.out.println(text);
        js.executeScript("console.log('test')");
    }

    /**
     * 执行脚本，在控制台打印文本内容
     */
    @Test
    public void executeScript3(){
        JavascriptExecutor js = (JavascriptExecutor)driver;
        js.executeScript("console.log('test')");
    }

    /**
     *  备注：要求浏览器处于无头模式
     */
    @Test
    public void printPage() throws Exception{
        driver.quit();
        driver = commonDemo.getChromeDriverWithHeadlessMode();
        String url = commonDemo.getUserDir("/src/main/resources/windowHandle.html");
        driver.get(url);
        PrintsPage printer = (PrintsPage)driver;
        PrintOptions printOptions = new PrintOptions();
        printOptions.setPageRanges("1-2");
        Pdf pdf = printer.print(printOptions);
        String content = pdf.getContent();
        System.out.println(content);
    }

    @AfterTest
    public void tearDown() throws Exception{
        Thread.sleep(2000);
        driver.quit();
    }
}
